package cn.sdhuijin.healthexam.control;

import android.app.Activity;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import cn.sdhuijin.healthexam.DatabaseHelper;
import cn.sdhuijin.healthexam.R;
import cn.sdhuijin.healthexam.activity.AbstractActivity;
import cn.sdhuijin.healthexam.bean.User;
import cn.sdhuijin.healthexam.ui.popup.DateTimePickerDialog;

/**
 * 修改完毕
 * 
 * @author houyuchun
 * 
 */
public class AbstractControlActivity extends AbstractActivity {

	protected ControlManager controlManager = new ControlManager();

	/**
	 * 初始化RadioGroupView
	 * 
	 * @param radGroupId
	 * @param propName
	 */
	protected void addRadioGroupView(int radGroupId, String propName) {
		controlManager.addControl(new RadioGroupViewControl(this, radGroupId,
				propName));
	}

	/**
	 * 初始化EditTextView
	 * 
	 * @param editTextId
	 * @param propName
	 */
	protected void addEditTextView(int editTextId, String propName) {
		controlManager.addControl(new EditTextViewControl(this, editTextId,
				propName));
	}

	/**
	 * 初始化RadioCheckUnionView
	 * 
	 * @param checkIds
	 * @param propNames
	 * @param radGroupId
	 * @param enableStr
	 */
	protected void addRadioCheckUnionView(int[] checkIds, String[] propNames,
			int radGroupId, String enableStr) {
		controlManager.addControl(new RadioCheckUnionViewControl(this,
				checkIds, propNames, radGroupId, enableStr));
	}

	/**
	 * 初始化CheckBoxView
	 * 
	 * @param checkIds
	 * @param propName
	 */
	protected void addCheckBoxView(int[] checkIds, String propName) {
		controlManager.addControl(new CheckBoxViewControl(this, checkIds,
				propName));
	}

	/**
	 * 初始化CheckBoxgroupView
	 * 
	 * @param checkIds
	 * @param propName
	 * @param etId
	 * @param etName
	 */
	protected void addCheckBoxGroupView(int[] checkIds, String propName,
			int etId, String etName, String enableStr) {
		controlManager.addControl(new CheckBoxGroupViewControl(this, checkIds,
				propName, etId, etName, enableStr));
	}

	/**
	 * 初始化RadioEditUnionView
	 * 
	 * @param edtIds
	 * @param propName
	 * @param editTextNames
	 * @param radGroupId
	 * @param enableStr
	 */
	protected void addRadioEditUnionView(int[] edtIds, String propName,
			String[] editTextNames, int radGroupId, String enableStr) {
		controlManager.addControl(new RadioEditUnionViewControl(this, edtIds,
				propName, editTextNames, radGroupId, enableStr));
	}

	/**
	 * 初始化TextView
	 * 
	 * @param textViewId
	 * @param propName
	 */
	protected void addTextView(int textViewId, String propName) {
		controlManager.addControl(new TextViewControl(this, textViewId,
				propName));
	}

	/**
	 * 通过录入日期查找当前编辑档案人员信息
	 * 
	 * @param createDate
	 *            录入日期
	 */
	protected void setCurrentEditUserName(String createDate) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this);
		String name = databaseHelper.getCurrentEditUserInfo(createDate);
		TextView txDoctor = (TextView) findViewById(R.id.duty_doctor);
		txDoctor.setText(User.getCurrentUser().getName());
		TextView txPatient = (TextView) findViewById(R.id.visit_patient);
		txPatient.setText(name);
	}

	/**
	 * 日期选择控件
	 * 
	 * @param editTextId
	 */
	protected void setEditTextListener(int editTextId) {
		final EditText editText = (EditText) findViewById(editTextId);
		editText.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				hideInputKeyboard(v);
				DateTimePickerDialog dialog = new DateTimePickerDialog(
						AbstractControlActivity.this);
				dialog.dateTimePicKDialog(editText, 1);
			}
		});

		editText.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View arg0, boolean arg1) {
				if (arg1) {
					hideInputKeyboard(arg0);
					DateTimePickerDialog dialog = new DateTimePickerDialog(
							AbstractControlActivity.this);
					dialog.dateTimePicKDialog(editText, 1);
				}
			}
		});
	}

	/**
	 * 隐藏输入键盘
	 * 
	 * @param edt
	 */
	private void hideInputKeyboard(View edt) {
		try {
			InputMethodManager im = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
			IBinder windowToken = edt.getWindowToken();
			if (windowToken != null) {
				im.hideSoftInputFromWindow(windowToken, 0);
			}
		} catch (Exception e) {

		}
	}
}
